@echo off
REM Manga Colorizer Windows Installation Script
REM This script sets up the entire manga colorizer system on Windows

echo 🎨 Manga Colorizer Windows Installation Script
echo ==============================================

REM Check if Docker is installed
docker --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Docker is not installed. Please install Docker Desktop first.
    pause
    exit /b 1
)

REM Check if Docker Compose is installed
docker-compose --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Docker Compose is not installed. Please install Docker Desktop which includes Docker Compose.
    pause
    exit /b 1
)

REM Create necessary directories
echo 📁 Creating directories...
if not exist "server\models" mkdir server\models
if not exist "server\uploads" mkdir server\uploads
if not exist "deployments\nginx\ssl" mkdir deployments\nginx\ssl

REM Create .env file if it doesn't exist
if not exist "server\.env" (
    echo 📝 Creating .env file...
    copy server\.env.example server\.env
    echo ⚠️  Please edit server\.env with your configuration
)

REM Create nginx configuration
echo 🔧 Creating nginx configuration...
(
echo events {
echo     worker_connections 1024;
echo }
echo.
echo http {
echo     upstream api {
echo         server api:8000;
echo     }
echo.
echo     server {
echo         listen 80;
echo         server_name localhost;
echo.
echo         location / {
echo             proxy_pass http://api;
echo             proxy_set_header Host $host;
echo             proxy_set_header X-Real-IP $remote_addr;
echo             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
echo             proxy_set_header X-Forwarded-Proto $scheme;
echo         }
echo.
echo         location /static {
echo             alias /app/uploads;
echo             expires 1d;
echo             add_header Cache-Control "public, immutable";
echo         }
echo     }
echo }
) > deployments\nginx\nginx.conf

REM Build and start services
echo 🚀 Building and starting services...
docker-compose build
docker-compose up -d

REM Wait for services to be ready
echo ⏳ Waiting for services to be ready...
timeout /t 30 /nobreak >nul

REM Check if services are running
echo 🔍 Checking service health...
curl -f http://localhost:8000/api/v1/health >nul 2>&1
if %errorlevel% equ 0 (
    echo ✅ API service is running
) else (
    echo ❌ API service is not responding
    echo Check logs with: docker-compose logs api
)

echo.
echo 🎉 Installation complete!
echo.
echo Services are running at:
echo   - API: http://localhost:8000
echo   - API Docs: http://localhost:8000/docs
echo   - MinIO Console: http://localhost:9001
echo.
echo To stop services: docker-compose down
echo To view logs: docker-compose logs -f
echo To restart: docker-compose restart
echo.
pause
